package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    /**
     * 登录逻辑：
     * 1.根据用户名和密码判断
     * 2.如果有数据，返回success
     * 3.如果没有数据，返回error
     *
     * @param userModel
     * @return
     */
    @Override
    public ResultModel login(UserModel userModel) {
//        如果有验证码，则将验证码写在这里
        ResultModel resultModel = new ResultModel();
//        List<UserModel> list=new ArrayList<>();

//
        UserModel model = userDao.findByUsernameAndPassword(userModel);
        boolean isHave = model != null;
        if (isHave) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel add(UserModel userModel) {
//        int result = userDao.add(userModel);
//        ResultModel resultModel = new ResultModel();
//        if (result > 0) {
//            resultModel.setMsg("success");
//        } else {
//            resultModel.setMsg("error");
//        }
        return new ResultModel(userDao.add(userModel) > 0 ? ResultModel.SUCCESS : ResultModel.ERROR);
    }

    @Override
    public ResultModel update(UserModel userModel) {
        return new ResultModel(userDao.update(userModel) > 0 ? ResultModel.SUCCESS : ResultModel.ERROR);
    }

    @Override
    public ResultModel delete(UserModel userModel) {
        return new ResultModel(userDao.delete(userModel) > 0 ? ResultModel.SUCCESS : ResultModel.ERROR);
    }

    @Override
    public ResultModel findById(UserModel userModel) {

        return new ResultModel(userDao.findById(userModel));
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        return new ResultModel(userDao.findAll(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        return new ResultModel(userDao.getCount(userModel));
    }
}
